package com.sdvdxl.action;

import java.util.ArrayList;
import java.util.List;

import com.sdvdxl.model.BlogTypeEntity;
import com.sdvdxl.service.BlogTypeService;
import com.sdvdxl.util.JsonResult;

public class BlogTypeAction extends BaseAction {
	private BlogTypeEntity blogTypeEntity;
	private BlogTypeService blogTypeService;

	public BlogTypeEntity getBlogTypeEntity() {
		return blogTypeEntity;
	}

	public void setBlogTypeEntity(BlogTypeEntity blogTypeEntity) {
		this.blogTypeEntity = blogTypeEntity;
	}

	public BlogTypeService getBlogTypeService() {
		return blogTypeService;
	}

	public void setBlogTypeService(BlogTypeService blogTypeService) {
		this.blogTypeService = blogTypeService;
	}

	/**
	 * 添加博文分类
	 */
	public void addBlogType() {
		JsonResult result = null;
		try {
			result = blogTypeService.add(blogTypeEntity);
			//更新application中的分类
			List<BlogTypeEntity> blogTypes = blogTypeService.getAll();
			application.setAttribute("blogTypes", blogTypes);
		} catch(Exception e) {
			result = new JsonResult();
			result.setMsg("添加失败！");
			e.printStackTrace();
		}
		writeJson(result);
	}
	
	/**
	 * 删除博文分类
	 */
	public void delBlogType() {
		JsonResult result = null;
		try {
			result = blogTypeService.del(blogTypeEntity);
			//更新application中的分类
			List<BlogTypeEntity> blogTypes = blogTypeService.getAll();
			application.setAttribute("blogTypes", blogTypes);
		} catch(Exception e) {
			result = new JsonResult();
			result.setMsg("删除失败！");
			e.printStackTrace();
		}
		writeJson(result);
	}
	
	/**
	 * 取得所有的博文分类
	 * @return 博文分类列表
	 */
	public String getAllBlogTypes() {
		List<BlogTypeEntity> blogTypes = new ArrayList<BlogTypeEntity>();
		try {
			blogTypes = blogTypeService.getAll();
		} catch(Exception e) {
			e.printStackTrace();
		}
		request.setAttribute("getAllBlogTypes", blogTypes);
		return "getAllBlogTypes";
	}
	
}
